package downloader

import org.slf4j.LoggerFactory
import us.codecraft.webmagic.ResultItems
import us.codecraft.webmagic.Task
import us.codecraft.webmagic.pipeline.Pipeline
import java.io.File

class FilePipeline(val bookDir: File) : Pipeline {
    val Log = LoggerFactory.getLogger(javaClass)
    override fun process(r: ResultItems, task: Task?) {
        val title: String? = r["title"]
        val c: String? = r["content"]
        val idx = r.get<Int>("idx")

        if (title != null && c != null) {
            Log.info("$idx")
            val f = File(bookDir!!, "$idx")
            if(f.exists())
                f.delete()
            val pw = f.printWriter()
            pw.use {
                it.println(title)
                it.println(c)
                it.println()
            }
        }


    }

}